﻿using System;
using System.Linq;
using Newtonsoft.Json;
using FrameWork.Services;
using System.Windows.Data;
using FrameWork.Core.Enum;
using System.Globalization;
using System.Collections.Generic;

namespace AncheStart.Converter
{
    public class UnitsConverter : IValueConverter
    {
        public object Convert(object stepIndex, Type targetType, object parameter, CultureInfo culture)
        {

            try
            {
                var columnCode = System.Convert.ToInt32(parameter);
                var unitsInfo = GlobalConstant.UnitsInfos.Where(P => (int)P.UnitType == columnCode).FirstOrDefault();
                var unitList = JsonConvert.DeserializeObject<List<EnumberEntity>>(unitsInfo.UnitCode);
                var selectUnit = unitList.Where(p => p.EnumValue == unitsInfo.SelectUnitValue).FirstOrDefault();
                return unitsInfo.UnitName + "(" + selectUnit.Desction + ")";
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var data = (bool)value;
            if (data)
            {
                return System.Convert.ToInt32(parameter);
            }
            return -1;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}